#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <err.h>
#include <string.h>

#include <regex.h>

#define MAXBUFSZ        1024

enum {
    TFS_RE_MATCH,
    TFS_RE_NOMATCH,
    TFS_RE_ERR,
};

int tfs_re_init(char *str, char *re);
int tfs_re_match(char *str, char *re);

    int 
tfs_regexp(char *str, char *re)
{
    regex_t preg;

    int rv = 0;
    int status = 0;
    char *errbuf = NULL;

    if (re == NULL)
        return (TFS_RE_ERR);

    if ( (errbuf = (char *)calloc(MAXBUFSZ, 1)) == NULL) {
        warn("calloc");
        return (TFS_RE_ERR);
    }

    if ( (rv = regcomp(&preg, re, REG_EXTENDED|REG_NOSUB)) != 0) {
        (void)regerror(rv, &preg, errbuf, MAXBUFSZ);
        return (TFS_RE_ERR);
    }

    rv = regexec(&preg, str, 0, NULL, 0);
    switch (rv) {
        case 0:
            status = TFS_RE_MATCH;
            break;
        case REG_NOMATCH:
            status = TFS_RE_NOMATCH;
            break;
        default:
            status = TFS_RE_ERR;
            break;
    }

    free(errbuf);
    return (status);
}
